//
//  MLXMediatorHeader.h
//  Pods
//
//  Created by MengLingChao on 2018/7/8.
//

#ifndef MLXMediatorHeader_h
#define MLXMediatorHeader_h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

static NSMutableDictionary *modulesDictionary = nil;

/**根据传入的组件协议返回实现该协议的类的对象*/
NS_INLINE id MLXModuleInstanceFromProtocol(Protocol *protocol) {
    NSString *className = NSStringFromProtocol(protocol);
    Class aClass = NSClassFromString(className);
//    NSLog(@"class %@,%@",className,aClass);
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        modulesDictionary = [NSMutableDictionary dictionary];
    });
    id module = modulesDictionary[className];
    if (!module) {
        module = [[aClass alloc]init];
        modulesDictionary[className] = module;
    }
    if ([module conformsToProtocol:protocol]){
        return module;
    }
    return nil;
}
#define MLXModuleInstance(module,MLXModuleProtocol) id<MLXModuleProtocol> module = MLXModuleInstanceFromProtocol(@protocol(MLXModuleProtocol));

#endif /* MLXMediatorHeader_h */
